// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        
        moveSpeed: 0,
        accel: 0,

        bulletPrefab: {
            default: null,
            type: cc.Prefab
        },

        boomAudio: {
            default: null,
            type: cc.AudioClip
        },

        level: 0,
    },

    // LIFE-CYCLE CALLBACKS:

    onKeyDown(event){
        switch(event.keyCode){
            case cc.macro.KEY.a:
                this.accLeft = true;
                break;
            case cc.macro.KEY.d:
                this.accRight = true;
                break;
            case cc.macro.KEY.w:
                this.accUp = true;
                break;
            case cc.macro.KEY.s:
                this.accDown = true;
                break;
        }
    },

    onKeyUp(event){
        switch(event.keyCode){
            case cc.macro.KEY.a:
                this.accLeft = false;
                break;
            case cc.macro.KEY.d:
                this.accRight = false;
                break;
            case cc.macro.KEY.w:
                this.accUp = false;
                break;
            case cc.macro.KEY.s:
                this.accDown = false;
                break;
        }
    },

    onLoad () {


        this.accLeft = false;
        this.accRight = false;

        this.xSpeed = 0;
        this.timer = 0;
        this.lastTimer = 0;
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    },

    onDestroy(){

        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    },

    initInfo(game){
        this.game = game;
        this.minPosX = - game.gameLayet.width / 2;
        this.maxPosX = game.gameLayet.width / 2;
        this.minPosY = - game.gameLayet.height / 2;
        this.maxPosY = game.gameLayet.height / 2 - this.node.height;
        this.bulletManager = this.game.bulletManagerInstance.getComponent('BulletManager');
    },

    start () {

    },

    spawnNewBullet: function() {
        // 使用给定的模板在场景中生成一个新节点
        var newBullet = cc.instantiate(this.bulletPrefab);
        // 将新增的节点添加到 Canvas 节点下面
        this.node.parent.addChild(newBullet);

        newBullet.setPosition(cc.v2(this.node.x,this.node.y + this.node.height + newBullet.height / 2));

        newBullet.getComponent('Bullet').initInfo(this.level);
    },

    playDeadAudio(){
        cc.audioEngine.playEffect(this.boomAudio, false);
    },

    update (dt) {

        if(this.game == null || this.game.gameOver) return;

        this.timer += dt;
        this.level = Math.round(this.timer / 10);

        if (this.accLeft) {
            this.xSpeed = -this.moveSpeed;
        }else if (this.accRight) {
            this.xSpeed = this.moveSpeed;
        }
        else{
            this.xSpeed = 0;
        }

        if (this.accUp) {
            this.ySpeed = this.moveSpeed;
        }else if (this.accDown) {
            this.ySpeed = -this.moveSpeed;
        }
        else{
            this.ySpeed = 0;
        }

        if (this.timer - this.lastTimer > 0.2) {
            this.lastTimer = this.timer;
            this.bulletManager.drawBullet();
        }

        if (this.node.x + this.xSpeed < this.minPosX) {
            this.node.x = this.minPosX;
        }else if (this.node.x + this.xSpeed > this.maxPosX) {
            this.node.x = this.maxPosX;
        }else{
            this.node.x += this.xSpeed;
        }

        if (this.node.y + this.ySpeed < this.minPosY) {
            this.node.y = this.minPosY;
        }else if (this.node.y + this.ySpeed > this.maxPosY) {
            this.node.y = this.maxPosY;
        }else{
            this.node.y += this.ySpeed;
        }

    },

    onBeginContact: function (contact, selfCollider, otherCollider) {

        if (otherCollider.node.group == 'Enemy') {
            this.game.setGameOver();
            this.playDeadAudio();
            this.game.drawEffect(this.node, 1, 'boom1');
            this.node.removeFromParent();
        }

    },
});
